<template>
  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="70%" @ok="handleSubmit"
    :okButtonProps="{ loading: loading }">

    <BasicForm @register="registerForm">



    </BasicForm>


  </BasicModal>
</template>
<script lang="ts" setup>
import { ref, computed, unref, h, toRaw, reactive } from 'vue';
import { BasicForm, useForm } from '@/components/Form';
import { Button } from 'ant-design-vue';
import { TableAction } from '@/components/Table';


import {
  set_category_formSchema, optionColumns
} from './role.data';

import { BasicModal, useModalInner } from '@/components/Modal';
import { Form, FormItem, Input, InputNumber, DatePicker, Select, Switch } from 'ant-design-vue';

import { questionBatchCategoryId, questionOptions } from '@/api/heng/ex';

import {
  BasicTableProps,
  VxeBasicTable,
  VxeGridInstance,
  VxeGridListeners,
  VxeTableDefines,
} from '@/components/VxeTable';


import { useMessage } from "@/hooks/web/useMessage";
import util from '@/utils/util';

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const { createMessage } = useMessage();


const id = ref("")

const options = ref([]);


const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
  labelWidth: 100,
  schemas: set_category_formSchema,
  showActionButtonGroup: false,
  baseColProps: { lg: 12, md: 24 },
});


const dataList = ref([]);
const loading = ref(false);


const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //打开弹层后的回调
  // resetFields();
  setModalProps({ confirmLoading: false });
  resetFields();
  // console.log("--data.rows:", data.rows);

  dataList.value = data.rows;
});

const getTitle = computed(() => ('选择分类'));


//async function handleSubmit()

async function handleSubmit() {
  try {

    const values = await validate();
    let arr = [];
    dataList.value.forEach((item) => {
      arr.push(item.questionId);
    });
    let questionIds = arr.join(",");

    // console.log("----questionIds:", questionIds);
    values.questionIds = questionIds;
    loading.value = true;

    questionBatchCategoryId(values).then(res => {
      sunccFn(res);
    }).finally(() => {
      loading.value = false;
    });

  } catch (e) {
    console.log(e)
  } finally {
    setModalProps({ confirmLoading: false });

  }
}

function sunccFn(result) {
  if (result && result.code == 200) {
    createMessage.success(result.message);
  } else {
    createMessage.error(result.message);
  }
  closeModal()
  emit('success');
}

function cancelFn() {
  //  tableRef.value.clearValidate();
}

function getQuestionOptions() {
  questionOptions({ questionId: id.value }).then(res => {
    if (res.code == 200) {
      options.value = res.data;
      tableRef.value.reloadData(res.data);
    } else {
      createMessage.error(result.message);

    }

  });
}
function addFn() {
  tableRef.value.insertAt({ questionId: id.value, isTrue: 0 }, -1);
}

function getActions(record) {
  let arr = [

    {
      // icon: 'ant-design:delete-outlined',
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否删除？',
        placement: 'left',
        confirm: handleClose.bind(null, record),
      },
    }
  ];


  return arr;
}

function handleClose(record: Recordable) {

  tableRef.value.remove(record);
  //console.log(record);

  // questionRemove(record)
  //   .then((res) => {
  //     if (res.code == 200) {
  //       createMessage.success(res.message);
  //       tableRef.value.commitProxy('reload');

  //     } else {
  //       createMessage.error(res.message);

  //     }

  //   })
  //   .catch((res) => {
  //     createMessage.error(res.message);
  //   })
  //   .finally(() => {
  //   });
}

</script>
<style lang="less">
.item-row {
  margin: 0 auto;

  //padding-left: 5%;
  .ant-form-item-label {
    flex: 0 0 28.5%;
    max-width: 28.5%;
  }
}

.lqfp-8 {
  //padding-left: 9% !important;

}
</style>
